package com.zhiche.lisa.oms.controller;

import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.oms.service.ILocationService;
import com.zhiche.lisa.oms.vo.LocationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 地址 接口
 * @since 2018-06-01
 * @author lbl
 */
@RestController
@RequestMapping("/location")
@Api(value = "/location", description = "地址类型接口",tags={"地址类型接口"})
public class LocationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LocationController.class);

    @Autowired
    private ILocationService locationService;

    @GetMapping(value="/getLocationByValue")
    @ApiOperation(value = "获取各类地址信息", notes = "获取各类地址信息", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<LocationVo> getLocationByValue(String typeValue, String value) {
        LOGGER.info("LocationController.getLocation（根据 val 获取发卡方类型） params : {} {}", typeValue,value);
        RestfulResponse<LocationVo> result = new RestfulResponse<>(0,"success",null);
        try {
            result.setData(locationService.getLocationByValue(typeValue,value));
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.getLocationByValue ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        }
        return result;
    }

    @PostMapping(value="/insertLocation")
    @ApiOperation(value = "新增各类地址信息", notes = "新增各类地址信息", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<LocationVo> insertLocation(String typeValue,String value,String label) {
        LOGGER.info("LocationController.insertLocation（新增各类地址信息） params : {} {}", typeValue,value);
        RestfulResponse<LocationVo> result = new RestfulResponse<>(0,"success",null);
        try {
            result.setData(locationService.insertOrUpdateLocation(typeValue, value, label, null, new Long(1)));
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.getLocationByValue ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        }
        return result;
    }

    @GetMapping(value="/getLocationById")
    @ApiOperation(value = "根据ID获取地址信息", notes = "根据ID获取地址信息", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<LocationVo> getLocationById(Long id) {
        LOGGER.info("LocationController.getLocation（根据ID获取地址信息） params : {} ",id);
        RestfulResponse<LocationVo> result = new RestfulResponse<>(0,"success",null);
        try {

            result.setData(locationService.getLocationById(id));
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.getLocationByValue ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        }
        return result;
    }

    @GetMapping(value = "/getDriverLocationById")
    @ApiOperation(value = "司机根据ID获取地址信息", notes = "根据ID获取地址信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<LocationVo> getDriverLocationById(Long id) {
        LOGGER.info("LocationController.getLocation（根据ID获取地址信息） params : {} ", id);
        RestfulResponse<LocationVo> result = new RestfulResponse<>(0, "success", null);
        try {

            result.setData(locationService.getDriverLocationById(id));
        } catch (BaseException e) {
            LOGGER.error("CardIssuerTypeController.getLocationByValue ERROR... ", e);
            result = new RestfulResponse<>(e.getCode(), e.getMessage(), null);
        }
        return result;
    }

}
